﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCCommon.Enums;
using BCData.WeChatComponent.WeChatCustomMenu;
using BCDto.WeChatComponent.WeChatCustomMenu;
using BCEntity.WeChatComponent.WeChatCustomMenu;
using BCService.Utils;
using WeChatSDK.WeChatDto.Button;
using WeChatSDK.WeChatServiceComponent.Button;

namespace BCService.WeChatComponent.WeChatCustomMenuService
{
    /// <summary>
    /// 微信自定义菜单服务
    /// </summary>
    public class WeChatCustomMenuService : IWeChatCustomMenuService
    {
        private readonly IWeChatCustomMenuData weChatCustomMenuData;
        private readonly ButtonService buttonService;
        public WeChatCustomMenuService(IWeChatCustomMenuData weChatCustomMenuData)
        {
            this.weChatCustomMenuData = weChatCustomMenuData;
            this.buttonService = new ButtonService();
        }

        public WeChatCustomMenuDto Add(WeChatCustomMenuRequestDto weChatCustomMenuRequestDto)
        {
            CheckInfo(weChatCustomMenuRequestDto,null);
            WeChatCustomMenuEntity weChatCustomMenuEntity = weChatCustomMenuRequestDto.As<WeChatCustomMenuEntity>();
            return this.weChatCustomMenuData.Add(weChatCustomMenuEntity).As<WeChatCustomMenuDto>();

        }
        
        public void Delete(int weChatCustomMenuId)
        {
            WeChatCustomMenuEntity weChatCustomMenuEntity = this.weChatCustomMenuData.Get(weChatCustomMenuId);
            if (weChatCustomMenuEntity.Level == 1)
            {
                var list = this.weChatCustomMenuData.GetList().Where(p => p.ParentMenuId == weChatCustomMenuEntity.WeChatCustomMenuId).Select(p => p.WeChatCustomMenuId);
                foreach (int id in list)
                {
                    this.Delete(id);
                }
            }
            this.weChatCustomMenuData.Delete(weChatCustomMenuId);
        }

        public IEnumerable<WeChatCustomMenuDto> GetList()
        {
            return this.weChatCustomMenuData.GetList().As<IEnumerable<WeChatCustomMenuDto>>();
        }
       
        public WeChatCustomMenuDto Update(int weChatCustomMenuId, WeChatCustomMenuRequestDto weChatCustomMenuRequestDto)
        {
            if (!this.weChatCustomMenuData.CheckMenuExists(weChatCustomMenuId))
            {
                throw new ArgumentException("此菜单并不存在。");
            }
            CheckInfo(weChatCustomMenuRequestDto, weChatCustomMenuId);
            WeChatCustomMenuEntity entity = weChatCustomMenuRequestDto.As<WeChatCustomMenuEntity>();
            entity.WeChatCustomMenuId = weChatCustomMenuId;
            return this.weChatCustomMenuData.Update(entity).As<WeChatCustomMenuDto>();
        }
        public bool RemoveWeChatMenu()
        {
            WeChatHttpResponseHandle.ProcessMessage(this.buttonService.DeleteCostomMenu());
            return true;
        }
        public bool PushMenu()
        {

            ButtonDto button = this.GetWeChatCustomMenuList(this.GetList());
            this.RemoveWeChatMenu();
            WeChatHttpResponseHandle.ProcessMessage(this.buttonService.CreateCostomMenu(button));
            return true;
        }
        private ButtonDto GetWeChatCustomMenuList(IEnumerable<WeChatCustomMenuDto> weChatCustomMenuDtos)
        {
            ButtonDto button = new ButtonDto();
            button.Button = this.GetButtons(weChatCustomMenuDtos);
            return button;
        }
        private List<ButtonBaseDto> GetButtons(IEnumerable<WeChatCustomMenuDto> weChatCustomMenuDtos, int level = 1)
        {
            List<ButtonBaseDto> buttons = new List<ButtonBaseDto>();
            foreach (WeChatCustomMenuDto item in weChatCustomMenuDtos.Where(p => p.Level == level))
            {
                ButtonBaseDto buttonBaseDto;
                int childrenLevel = item.Level + 1;
                if (item.Type == WeChatCustomMenuType.media_id || item.Type == WeChatCustomMenuType.view_limited)
                {
                    buttonBaseDto = new MediaButtonDto()
                    {
                        MediaId = item.MediaId,
                        Name = item.Name,
                        Type = item.Type.ToString(),
                        SubButton = GetButtons(weChatCustomMenuDtos.Where(p => p.ParentMenuId == item.WeChatCustomMenuId), childrenLevel)
                    };
                }
                else if (item.Type == WeChatCustomMenuType.miniprogram)
                {
                    buttonBaseDto = new MiniprogramButtonDto()
                    {
                        Appid = item.Appid,
                        Pagepath = item.PagePath,
                        Url = item.Url,
                        Name = item.Name,
                        Type = item.Type.ToString(),
                        SubButton = GetButtons(weChatCustomMenuDtos.Where(p => p.ParentMenuId == item.WeChatCustomMenuId), childrenLevel)
                    };
                }
                else if (item.Type == WeChatCustomMenuType.view)
                {
                    buttonBaseDto = new ViewButtonDto()
                    {
                        Url = item.Url,
                        Name = item.Name,
                        Type = item.Type.ToString(),
                        SubButton = GetButtons(weChatCustomMenuDtos.Where(p => p.ParentMenuId == item.WeChatCustomMenuId), childrenLevel)
                    };
                }
                else
                {
                    buttonBaseDto = new ClickButtonDto()
                    {
                        Key = item.ClickKey,
                        Name = item.Name,
                        Type = item.Type.ToString(),
                        SubButton = GetButtons(weChatCustomMenuDtos.Where(p => p.ParentMenuId == item.WeChatCustomMenuId), childrenLevel)
                    };
                }
                buttons.Add(buttonBaseDto);
            }
            return buttons;
        }
        private void CheckInfo(WeChatCustomMenuRequestDto weChatCustomMenuRequestDto,int? weChatCustomMenuId)
        {
            int count = this.weChatCustomMenuData.GetMenuCount(weChatCustomMenuRequestDto.Level, weChatCustomMenuRequestDto.ParentMenuId);
           
            if (weChatCustomMenuRequestDto.Level == 1)
            {
                if (count >= 3)
                {
                    throw new ArgumentException("一级菜单最多设置3个。");
                }
                if (weChatCustomMenuRequestDto.Name.Length > 10)
                {
                    throw new ArgumentException("一级菜单最多输入10个字符,单最多只会显示4个字符。");
                }
                weChatCustomMenuRequestDto.ParentMenuId = null;
            }
            else
            {
                if (count >= 5)
                {
                    throw new ArgumentException("二级菜单最多设置5个。");
                }
                if (weChatCustomMenuRequestDto.Name.Length > 10)
                {
                    throw new ArgumentException("二级菜单最多输入10个字符,单最多只会显示7个字符。");
                }
            }
            if (this.weChatCustomMenuData.CheckMenuExists(weChatCustomMenuRequestDto.Name, weChatCustomMenuRequestDto.Level))
            {
                if (weChatCustomMenuId.HasValue)
                {
                    WeChatCustomMenuEntity weChatCustomMenuEntity = this.weChatCustomMenuData.Get(weChatCustomMenuRequestDto.Name, weChatCustomMenuRequestDto.Level);
                    if (weChatCustomMenuEntity.WeChatCustomMenuId != weChatCustomMenuId)
                    {
                        throw new ArgumentException("此菜单名称已存在。");
                    }
                }
                else
                {
                    throw new ArgumentException("此菜单名称已存在。");
                }
            }
            if (weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.click)
            {
                if (string.IsNullOrWhiteSpace(weChatCustomMenuRequestDto.ClickKey))
                {
                    throw new ArgumentException("点击事件类型的菜单按钮必须填写命令值信息。");
                }
            }
            else if (weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.view)
            {
                if (string.IsNullOrWhiteSpace(weChatCustomMenuRequestDto.Url))
                {
                    throw new ArgumentException("跳转事件类型的菜单按钮必须填写链接信息。");
                }
            }
            else if (weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.miniprogram)
            {
                if (string.IsNullOrWhiteSpace(weChatCustomMenuRequestDto.Url))
                {
                    throw new ArgumentException("小程序事件类型的菜单按钮必须填写链接信息。");
                }
                if (string.IsNullOrWhiteSpace(weChatCustomMenuRequestDto.Appid))
                {
                    throw new ArgumentException("小程序事件类型的菜单按钮必须填写小程序Id信息。");
                }
                if (string.IsNullOrWhiteSpace(weChatCustomMenuRequestDto.PagePath))
                {
                    throw new ArgumentException("小程序事件类型的菜单按钮必须填写小程序页面路径信息。");
                }
            }
            else if (weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.media_id || weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.view_limited)
            {
                if (string.IsNullOrWhiteSpace(weChatCustomMenuRequestDto.MediaId))
                {
                    throw new ArgumentException("素材事件类型和跳转图文消息事件的菜单按钮必须选择素材。");
                }
            }
            else
            {
                if (weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.location_select)
                {
                    weChatCustomMenuRequestDto.ClickKey = "rselfmenu_2_0";
                }
                else if (weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.scancode_waitmsg)
                {
                    weChatCustomMenuRequestDto.ClickKey = "rselfmenu_0_0";
                }
                else if (weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.scancode_push)
                {
                    weChatCustomMenuRequestDto.ClickKey = "rselfmenu_0_1";
                }
                else if (weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.pic_sysphoto)
                {
                    weChatCustomMenuRequestDto.ClickKey = "rselfmenu_1_0";
                }
                else if (weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.pic_photo_or_album)
                {
                    weChatCustomMenuRequestDto.ClickKey = "rselfmenu_1_1";
                }
                else if (weChatCustomMenuRequestDto.Type == WeChatCustomMenuType.pic_weixin)
                {
                    weChatCustomMenuRequestDto.ClickKey = "rselfmenu_1_2";
                }
            }
        }

        public WeChatCustomMenuDto Get(int weChatCustomMenuId)
        {
            return this.weChatCustomMenuData.Get(weChatCustomMenuId).As<WeChatCustomMenuDto>();
        }
    }
}
